package org.zgflex.util
{
	/**
	 * A collections of usefull static methods.
	 * @author dankok
	 * 
	 */	
	public class Util
	{
		/**
		 * Applies properties set in config to a target object<br/> 
		 * @param config A config object
		 * @param target A target object to which a config is applied to
		 * @param reportError Report an error if any of properties specified in config doesn't exist among the object attributes
		 * @example The following code applies a config to a canvas:

<listing version="3.0">
var canvas:Canvas = new Canvas();
var config:Object = {
	 percentWidth: 100,
	 percentHeight: 50
}
Util.apply(config, canvas);
</listing>

@example The following code applies a config to a canvas, but produces an error because the attribute "foo" doesn't exist in the Canvas attributes (and reportError is set to <b>true</b>):

<listing version="3.0">
var canvas:Canvas = new Canvas();
var config:Object = {
	 foo: 'bar'
}
Util.apply(config, canvas, true, true);
</listing>

@example You can even define "inline" config, without a previous declaration:

<listing version="3.0">
var canvas:Canvas = new Canvas();
Util.apply({
	 percentWidth: 100,
	 percentHeight: 50
}, canvas, true, true);
</listing>
 		 * 
		 */		
		public static function apply(config:Object, target:Object, strongTyped:Boolean = true, reportErrors:Boolean = false):void {
			if (!config)
				return; 
			for (var p:String in config) {
			    trace ('Applying property "' + p + '": "' + config[p]);
			    if (config[p] != null && config[p] != undefined){ // do not apply nulls and NaNs
			    	if (strongTyped){ // checks if a target actually has this property
				    	if (target.hasOwnProperty(p)){ 
					    	target[p] = config[p];
					    } else if (reportErrors){
					    	throw new Error('Property "' + p + '" does not exist');
					    }
				    } else {
				    	target[p] = config[p];
				    }
			    }
			}		
		}
	}
}